##########################################################################
#  Copyright (C) 2007 Robert Connolly <rob_connolly@mailshack.com>       #
#                                                                        #
#  This program is free software; you can redistribute it and/or modify  #
#  it under the terms of the GNU General Public License as published by  #
#  the Free Software Foundation; either version 3 of the License, or     #
#  (at your option) any later version. See file COPYING for details.     #
#                                                                        #
#  This program is distributed in the hope that it will be useful,       #
#  but WITHOUT ANY WARRANTY; without even the implied warranty of        #
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
#  GNU General Public License for more details.                          #
#                                                                        #
#  You should have received a copy of the GNU General Public License     #
#  along with this program; if not, write to the                         #
#  Free Software Foundation, Inc.,                                       #
#  59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             #
##########################################################################

import dbus, dbus.glib, gtk, sys

sys.path.append("/home/robert/Desktop")

from cairolets.core import Sensor

class RhythmboxSensor(Sensor):
	"""<name>RhythmboxSensor</name>
	   <description>A Sensor to interact with the Rhythmbox Media Player</description>
	   <author email='rob_connolly@mailshack.com'>Robert Connolly</author>
	   <website>http://cairolets.googlecode.com</website>"""

	def __init__(self):
		bus = dbus.SessionBus()
		
		rb_start = False
		while rb_start == False:
			try:
				self.player = dbus.Interface(bus.get_object('org.gnome.Rhythmbox', '/org/gnome/Rhythmbox/Player'), 'org.gnome.Rhythmbox.Player')
				self.shell = dbus.Interface(bus.get_object('org.gnome.Rhythmbox', '/org/gnome/Rhythmbox/Shell'), 'org.gnome.Rhythmbox.Shell')
				rb_start = True
			except dbus.exceptions.DBusException:
				rb_start = False
				bus.start_service_by_name('org.gnome.Rhythmbox')
		
		bus.add_signal_receiver(self.__song_changed, 'playingUriChanged', 'org.gnome.Rhythmbox.Player', "org.gnome.Rhythmbox", "/org/gnome/Rhythmbox/Player")
		bus.add_signal_receiver(self.__status_changed, 'playingChanged', 'org.gnome.Rhythmbox.Player', "org.gnome.Rhythmbox", "/org/gnome/Rhythmbox/Player")
		
		self.was_playing = self.player.getPlaying()
		
		Sensor.__init__(self, None, False)
		
		self.__song_changed(self.player.getPlayingUri())
		
	def __song_changed(self, new_uri):
		try:
			info = self.shell.getSongProperties(new_uri)
			
			if 'rb:coverArt-uri' in info:
				cover = info['rb:coverArt-uri']
			else:
				cover = ''
			
			data = {'title': info['title'], 'artist': info['artist'],
				'album': info['album'], 'genre': info['genre'],
				'year': info['year'], 'description': info['description'],
				'duration': info['duration'], 'file-size': info['file-size'],
				'playing': self.player.getPlaying(), 'cover': cover}
			self.set_data(data)
		except dbus.exceptions.DBusException:
			data = {'title': '', 'artist': '',
				'album': '', 'genre': '',
				'year': '', 'description': '',
				'duration': '', 'file-size': '',
				'playing': False, 'cover': ''}
			self.set_data(data)
	
	def __status_changed(self, playing):
		if self.was_playing == playing:
			return
		else:
			self.edit_data('playing', playing)
		self.was_playing = playing
		
	def play_pause():
		self.player.playPause()
		
	def next():
		self.player.next()
		
	def previous():
		self.player.previous()
		
